<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/25
 * Time: 9:35
 */

class smtapi extends CI_Controller{
    function __construct() {
        parent::__construct();
        $this->load->model('Smt_model');
    }
    /**
     * 获取速卖通API URL
     * @param string $apiInfo(api函数地址)
     * @param string $code_arr(api查询条件参数)
     */
    public function getAliExpressSign($apiInfo_str='',$code_arr=''){
        $url = 'http://gw.api.alibaba.com:80/openapi/';//1688开放平台使用gw.open.1688.com域名
        $appKey = '9131907';;
        $appSecret ='LSOxdb16FHQ';
        /*$apiInfo = 'param2/1/aliexpress.open/alibaba.ae.warrantie.getList/' . $appKey;//此处请用具体api进行替换
        //配置参数，请用apiInfo对应的api参数进行替换
        $code_arr = array(
            'supplierId'=>'pandawill',
            'startBuyTime' => '2015-01-02 00:00:00',
            'endBuyTime' => '2016-01-02 00:00:00'
        );*/
        $info='param2/1/aliexpress.open/';
        $apiInfo=$info.$apiInfo_str.'/'.$appKey;
        $aliParams = array();
        $param_str='';//连接参数
        foreach ($code_arr as $key => $val) {
            $aliParams[] = $key . $val;
            $param_str .= $key . '=' . urlencode($val) . '&';
        }
        sort($aliParams);
        $sign_str = join('', $aliParams);
        $sign_str = $apiInfo . $sign_str;
        $code_sign = strtoupper(bin2hex(hash_hmac("sha1", $sign_str, $appSecret, true)));
        $apiurl=$url.$apiInfo.'?'.$param_str.'_aop_signature='.$code_sign;
        return $apiurl;
    }

    /**
     * urlget
     * @param $url
     * @return mixed
     */
    public function UrlGet($url){
        //初始化
        $ch = curl_init();

        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);

        //释放curl句柄
        curl_close($ch);
        //打印获得的数据
        return json_decode($output,true);
    }
    /**
     * 获取速卖通维保服务数据
     * @param $startBuyTime
     * @param $endBuyTime
     * @param int $pageSize
     * @param int $pageNo
     */
    public function GetAliExpressMaintenanceList($startBuyTime='',$endBuyTime='',$pageSize=200,$pageNo=1){
        $str='alibaba.ae.warrantie.getList';
        if($startBuyTime==''){
            $startBuyTime=date('Y-m-d 00:00:00',time());
        }
        if($endBuyTime==''){
            $endBuyTime=date('Y-m-d 23:00:00',time());
        }
        $arr=array(
            'supplierId'=>'pandawill',
            //'orderId'=>'',
            //'startBuyTime' => '2016-2-15 00:00:00',
            //'endBuyTime' => $endBuyTime,
            'startCreateTime'=>'2016-2-15 00:00:00',
            'endCreateTime'=>$endBuyTime,
            'pageSize'=>$pageSize,
            'pageNo'=>$pageNo
        );
        //获取速卖通保修服务URL
        $url=$this->getAliExpressSign($str,$arr);
        //获取速卖通保修服务数据
        $datalist=$this->UrlGet($url);
        return $datalist;
    }
    /**
     * 获取速卖通订单详情
     * @param $orderid
     */
    public function GetAliExpressOrder($orderid){
        $str='api.queryWarrantiesByOrderId';
        $arr=array(
            'supplierId'=>'pandawill',
            'orderId'=>$orderid
        );
        $url=$this->getAliExpressSign($str,$arr);
        $datalist=$this->UrlGet($url);
        return $datalist['dataList'];
    }
    public function test(){
        $OrderList=$this->GetAliExpressValidData();
        gsh_output($OrderList);
    }
    /**
     * 获取速卖通维保生效数据
     * @param string $startTime
     * @param string $endTime
     */
    public function GetAliExpressValidData($startTime='',$endTime='',$pageSize=200,$pageNo=1){
        $str='api.queryWarrantiesInforce';
        $arr=array(
            'supplierId'=>'pandawill',
            'startTime'=>$startTime,
            'endTime'=>$endTime,
            'pageSize'=>$pageSize,
            'pageNo'=>$pageNo
        );
        $url=$this->getAliExpressSign($str,$arr);
        $datalist=$this->UrlGet($url);
        return $datalist;
    }
    /**
     * 添加速卖通数据
     */
    public function AddAliExpressData(){
        set_time_limit(0);
        $Mdatalist=$this->GetAliExpressValidData(date('Y-m-d 00:00:00',time()),date('Y-m-d 00:00:00',time()));
        $page=ceil($Mdatalist['totalItem']/200);//获取数据总页数
        //循环获取所有数据
        if($page) {
            for ($i = 1; $i <= $page; $i++) {
                $Mdatalist = $this->GetAliExpressValidData(date('Y-m-d 00:00:00', time()), date('Y-m-d 00:00:00', time()), 200, $i);
                $AeData = $Mdatalist['dataList'];
                //对数据入库
                foreach ($AeData as $row) {
                    //查询订单详细数据
                    $AeOrderData = $this->GetAliExpressOrder($row['orderId']);
                    if ($AeOrderData[0]['warrantyStatus'] == 'create' || $AeOrderData[0]['warrantyStatus'] == 'finish') {
                        $orderid = $this->Smt_model->JudgeAliExpressOrderID($row['orderId']);
                        if ($orderid == '') {
                            $this->Smt_model->AddAliExpressWarrantyOrder($AeOrderData[0]);//添加订单详情
                        }
                    }
                }
            }
        }
    }
}